import sys
from PySide2.QtWidgets import (
    QApplication, QWidget, QVBoxLayout, QHBoxLayout,
    QLabel, QLineEdit, QPushButton, QFormLayout,
    QGraphicsDropShadowEffect
)
from PySide2.QtCore import Qt, QPropertyAnimation, QEasingCurve
from PySide2.QtGui import QColor, QFont


class ModernFormWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("现代化表单页面")
        self.resize(500, 400)
        self.setup_ui()
        self.set_style()

    def setup_ui(self):
        # 主布局
        main_layout = QVBoxLayout()
        main_layout.setAlignment(Qt.AlignCenter)
        main_layout.setSpacing(20)
        main_layout.setContentsMargins(30, 30, 30, 30)

        # 表单卡片（带阴影）
        form_card = QWidget()
        form_card.setObjectName("form_card")
        form_card.setFixedWidth(400)

        # 添加阴影效果
        shadow = QGraphicsDropShadowEffect()
        shadow.setBlurRadius(20)
        shadow.setColor(QColor(0, 0, 0, 80))
        shadow.setOffset(0, 5)
        form_card.setGraphicsEffect(shadow)

        # 表单布局
        form_layout = QFormLayout()
        form_layout.setSpacing(15)
        form_layout.setLabelAlignment(Qt.AlignRight)

        # 输入框
        self.name_input = QLineEdit()
        self.name_input.setPlaceholderText("请输入姓名")

        self.email_input = QLineEdit()
        self.email_input.setPlaceholderText("请输入邮箱")

        self.phone_input = QLineEdit()
        self.phone_input.setPlaceholderText("请输入手机号")

        # 添加到表单
        form_layout.addRow("姓 名：", self.name_input)
        form_layout.addRow("邮 箱：", self.email_input)
        form_layout.addRow("手 机：", self.phone_input)

        # 按钮布局
        btn_layout = QHBoxLayout()
        btn_layout.addStretch()

        self.submit_btn = QPushButton("提 交")
        self.reset_btn = QPushButton("重 置")

        btn_layout.addWidget(self.submit_btn)
        btn_layout.addWidget(self.reset_btn)

        # 将按钮布局加入表单
        form_layout.addRow(btn_layout)

        form_card.setLayout(form_layout)
        main_layout.addWidget(form_card)
        self.setLayout(main_layout)

        # 连接信号
        self.submit_btn.clicked.connect(self.on_submit)
        self.reset_btn.clicked.connect(self.on_reset)

    def set_style(self):
        # 设置整体样式
        self.setStyleSheet("""
            QWidget {
                background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                            stop:0 #a8edea, stop:1 #fed6e3);
                font-family: "Microsoft YaHei", Arial, sans-serif;
            }

            /* 表单卡片 */
            #form_card {
                background-color: white;
                border-radius: 15px;
                border: none;
            }

            /* 标签 */
            QLabel {
                color: #444;
                font-size: 14px;
                padding-right: 10px;
            }

            /* 输入框 */
            QLineEdit {
                height: 40px;
                padding: 8px 12px;
                border: 1.5px solid #d1d8e0;
                border-radius: 12px;
                font-size: 14px;
                background-color: #ffffff;
            }

            QLineEdit:focus {
                border-color: #6CB8FF;
                outline: none;
                box-shadow: 0 0 8px rgba(108, 184, 255, 0.3);
            }

            /* 按钮通用样式 */
            QPushButton {
                min-height: 40px;
                padding: 8px 24px;
                border-radius: 12px;
                border: none;
                font-size: 14px;
                font-weight: 500;
                color: white;
            }

            /* 提交按钮 - 淡蓝色 */
            QPushButton[flat="false"]#submit_btn {
                background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                    stop:0 #6CB8FF, stop:1 #4A9EFF);
            }

            QPushButton[flat="false"]#submit_btn:hover {
                background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
                                    stop:0 #5AA6FF, stop:1 #398CFF);
            }

            QPushButton[flat="false"]#submit_btn:pressed {
                padding-top: 9px;
                padding-bottom: 7px;
            }

            /* 重置按钮 - 浅灰 */
            QPushButton#reset_btn {
                background-color: #e0e0e0;
                color: #555;
            }

            QPushButton#reset_btn:hover {
                background-color: #d0d0d0;
            }
        """)

        # 手动设置按钮对象名（用于样式匹配）
        self.submit_btn.setObjectName("submit_btn")
        self.reset_btn.setObjectName("reset_btn")

    def on_submit(self):
        name = self.name_input.text().strip()
        email = self.email_input.text().strip()
        phone = self.phone_input.text().strip()

        if not name:
            self.show_message("请输入姓名！")
            return

        # 这里可以添加提交逻辑
        print(f"提交数据：{name}, {email}, {phone}")
        self.show_message("✅ 提交成功！", success=True)

    def on_reset(self):
        self.name_input.clear()
        self.email_input.clear()
        self.phone_input.clear()
        self.show_message("🔄 已重置表单")

    def show_message(self, text, success=False):
        from PySide2.QtWidgets import QLabel
        color = "#4CAF50" if success else "#F44336"
        # 你可以用 QMessageBox 或状态栏，这里简单用控制台+样式提示
        print(text)
        # 可扩展：添加一个临时 label 显示提示信息


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle("Fusion")  # 使用 Fusion 风格，兼容自定义样式

    window = ModernFormWindow()
    window.show()

    sys.exit(app.exec_())
